package com.wuxiaoqin.server.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.wuxiaoqin.server.entity.Users;
import com.wuxiaoqin.server.entity.loginParam.UserLoginParam;

import com.wuxiaoqin.server.service.IUsersService;


import com.wuxiaoqin.uitls.respObj.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.spec.InvalidKeySpecException;


@CrossOrigin
@RestController
@ApiOperation("登录注册")
public class LogRegController {
    @Autowired
    private IUsersService iUsersService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(UserLoginParam userLoginParam, HttpServletRequest request) throws InvalidKeySpecException, NoSuchAlgorithmException {
        return iUsersService.login(userLoginParam.getUsername(),userLoginParam.getPassword(),userLoginParam.getCode(),request);

    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public RespBean register(@RequestParam("username") String username,
                             @RequestParam("email") String email,
                             @RequestParam("phone") String phone,
                             @RequestParam("password") String password, HttpServletRequest request) throws InvalidKeySpecException, NoSuchAlgorithmException {

        RespBean respBean = iUsersService.register(username, email, phone, password, request);
         return respBean;
    }




    @ApiOperation(value = "手机号是否重复")
    @GetMapping("/register/{phone}")
    public RespBean ifNmae(@PathVariable("phone") String phone){
        return iUsersService.ifHaveName(phone);
    }




    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/user/info")
    public Object getUserInfo(Principal principal){
        if (principal == null){
            return null;
        }
        String phone = principal.getName();//获取用户名
        Users user = iUsersService.getUsersByUserPhone(phone);//通过用户名得到用户对象
        user.setUPswd("用户密码不能获取");
        user.setPrivatekey("用户私钥不能获取");
        System.out.println(user);
        return user;
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("退出成功");
    }

}
